package com.bose.bosecmdclient;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.bose.IBoseCmd;

public class CmdProxy {
    private static final String LOG_TAG = "BOSECMD_CLIENT";
    private static final String DEAULT_SERVICE_NAME_STR = "bosecmd_sys";
    private String mServiceName = DEAULT_SERVICE_NAME_STR;
    private IBoseCmd mBoseAidl;

    public CmdProxy(String srvName){
        if(null != srvName && srvName.length() > 0) {
            mServiceName = srvName;
        }
        registerSysService();
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(LOG_TAG, "onServiceConnected");
            mBoseAidl = IBoseCmd.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(LOG_TAG, "onServiceDisconnected");
            mBoseAidl = null;
        }
    };

    public void registerSysService() {
        IBinder binder = null;
        binder  = android.os.ServiceManager.getService(mServiceName);
        if (null == binder) {
             Log.d(LOG_TAG, "binder is null");
        } else {
            Log.d(LOG_TAG, "binder registered");
            mBoseAidl = IBoseCmd.Stub.asInterface(binder);
        }
    }

    public int CallSystem(String cmd) {
        int ret = -1;
        if(null == cmd || cmd.length() == 0) {
            return ret;
        }
        if(null == mBoseAidl) {
            registerSysService();
        }

        if (mBoseAidl != null) {
            try {
                ret = mBoseAidl.CallSystem(cmd);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        Log.d(LOG_TAG, "System() >>> " + cmd + "\n ret=" + ret);
        return ret;
    }

    public int CallPopen(String cmd, String resultStr) {
        byte[] result = null;
        int[] errCode = new int[1];
        errCode[0] = -666;

        if(null == cmd || cmd.length() == 0) {
                Log.e(LOG_TAG, "SendAudioMessage(), cmd is null!");
                return errCode[0];
        }

        if(null == mBoseAidl) {
            registerSysService();
        }

        if (mBoseAidl != null) {
            try {
                result = mBoseAidl.CallPopen(cmd, errCode);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        } else {
            Log.e(LOG_TAG, "mBoseAidl is null");
        }

        if (result != null) {
            resultStr = new String(result);
        }

        Log.d(LOG_TAG, "CallPopen():" + cmd + "\n errCode=" + errCode[0]
                   + ", result:\n"+ resultStr);
        return errCode[0];
    }

    public int SendAudioMessage(String cmd) {
        int ret = -1;
        if(null == cmd || cmd.length() == 0) {
            Log.e(LOG_TAG, "SendAudioMessage(), cmd is null!");
            return ret;
        }
        byte[] cmdBytes = cmd.getBytes();

        if(null == mBoseAidl) registerSysService();

        if (mBoseAidl != null) {
            try {
                ret = mBoseAidl.SendMessage(cmdBytes);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        } else {
            Log.d(LOG_TAG, "mBoseAidl is null");
        }

        Log.d(LOG_TAG, "SendAudioMessage() >>> " + cmd + " ret=" + ret);
        return ret;
    }

    public int GetAudioMessage(String cmd, String resultStr) {
        byte[] result = null;
        int[] errCode = new int[1];
        errCode[0] = -777;

        if(null == cmd || cmd.length() == 0) {
            return errCode[0];
        }
        byte[] cmdBytes = cmd.getBytes();

        if(null == mBoseAidl) registerSysService();

        if (mBoseAidl != null) {
            try {
                result = mBoseAidl.GetMessage(cmdBytes, errCode);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        } else {
            Log.d(LOG_TAG, "mBoseAidl is null");
        }

        if (result != null) {
            resultStr = new String(result);
        }

        Log.d(LOG_TAG, "GetAudioMessage():" + cmd + "\n errCode=" + errCode[0]
                   + ", result:\n"+ resultStr);
        return errCode[0];
    }
}